<?php
/**
 * 用户登陆 注册 注销类
 *@author  刘旭
 */
class log  extends core {
	/**
	*判断用户名是否唯一
	*@author 刘旭
	**/
	final static function unique_name($name){
		
	}

	/**
	*判断登陆
	*@author 刘旭
	**/
	static public function log_in(){
		$name=$_POST["name"];
		$password=$_POST["password"];
		$name=trim($name);
		$password=trim($password);
		$error=array();

		if(empty($name))
		{
			$error['user']="用户名不能为空！";
		}

		if(empty($password))
		{
			$error['password']="密码不能为空！";
		}
		
		//判断用户是否注册
		$user=core::selects('id,username,password',"user","username='$name'");
		if(empty($user))
		{
			$error["exist"]=$name."没有注册";
		}
		else
		{
			$user_id=$user[0]->id;
			$pw=$user[0]->password;
			if($password!=$pw){
				$error["pwd"]="密码错误,请重新输入";
			}else{
				//成功登陆
				$_SESSION["user"]=$name;
				$_SESSION["user_id"]=$user_id;
			}
		}
		if(!empty($error)){
			return $error;
		}

	}
	
	/**
	 * 注册函数
	 * @author 刘旭
	 */
	final static public function register(){
		$email    = $_POST['email'];
		$username = $_POST['username'];
		$pw1      = $_POST['pw1'];
		$pw2      = $_POST['pw2'];
		$error=array();
		if(empty($email)){
			$error["email"]="邮箱不能为空！";
		}
		if(empty($username)){
			$error["username"]="用户名不能为空！";
		}
		if(empty($pw1 )){
			$error["pw1"]="密码不能为空！";
		}
		if(empty($pw2 )){
			$error["pw2"]="重复密码不能为空！";
		}
		if($pw1!==$pw2){
			$error["same"]="两次密码不一致！";
		}
		if(empty($error)){
			$sql="insert into user(email,username,password) values('{$email}','{$username}','{$pw1}')";
			core::inserts($sql,null,true);
			$_SESSION["user"]=$username;
			$_SESSION["user_id"]=mysql_insert_id();
		}
		else 
		{
			return $error;
		}
		
	}
	
}
?>